var app = getApp();
var listOffset = 0;
var goodstype, sort, sort_type, searchval;
var win_width;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rangetype: 4,
    listhgt: 0,
    goods_width: 49,
    goods_pic_width: 100,
    goods_pic_rest_width: 100,
    isLoading: false,
    hasMore: true,
    goodslist: [],
    blankshow: false,
    blanktips: '~没有此类商品~'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    goodstype = options.goodstype;
    if (goodstype == 'search') {
      searchval = options.searchval;
    }
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var goods_height = res.windowWidth * that.data.goods_width * 0.01 * that.data.goods_pic_width * 0.01;
        win_width = res.windowWidth;
        var tmphgt = res.windowHeight - 45;
        that.setData({
          listhgt: tmphgt,
          goods_pic_height: goods_height
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.showLoading();
    listOffset = 0;
    sort = 'goods_id';
    sort_type = 'desc'
    this.loadList(listOffset, sort, sort_type);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  changeRangeTap: function (event) {
    var selectedval = parseInt(event.currentTarget.dataset.selected);
    var rtype = this.data["rangetype"];
    if (rtype == 1 && selectedval == 1) {
      return;
    }
    var new_rtype = rtype;
    if (selectedval == 1) {
      sort = 'sales_sum';
      new_rtype = 1;
      sort_type = 'asc';
    }
    else if (selectedval == 4) {
      sort = 'goods_id';
      new_rtype = 4;
      sort_type = 'desc';
    }
    else {
      sort = 'shop_price';
      if (rtype == 2) {
        new_rtype = 3;
        sort_type = 'asc';
      }
      else {
        new_rtype = 2;
        sort_type = 'desc';
      }
    }
    this.setData({
      rangetype: new_rtype
    });
    app.showLoading();
    listOffset = 0;
    this.loadList(listOffset, sort, sort_type);
  },
  changeDisplay: function () {
    var goods_width_val = this.data["goods_width"];
    var goods_pic_width_val;
    var goods_pic_rest_width_val;
    var goods_height;
    if (goods_width_val == 100) {
      goods_width_val = 49;
      goods_pic_width_val = 100;
      goods_pic_rest_width_val = 100;
    }
    else {
      goods_width_val = 100;
      goods_pic_width_val = 25;
      goods_pic_rest_width_val = 75;
    }
    goods_height = win_width * goods_width_val * 0.01 * goods_pic_width_val * 0.01;
    this.setData({
      goods_width: goods_width_val,
      goods_pic_width: goods_pic_width_val,
      goods_pic_height: goods_height,
      goods_pic_rest_width: goods_pic_rest_width_val
    });
  },
  loadMoreGoods: function () {
    if (this.data.hasMore == false) {
      return;
    }
    listOffset++;
    this.loadList(listOffset, sort, sort_type);
  },
  goodsTap: function (event) {
    var goodsid = event.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: '../goodsdetail/goodsdetail?goodsid=' + goodsid,
    });
  },
  loadList: function (offset, sort = 'shop_price', sort_asc = 'desc') {
    var url = '/index.php/api/Goods/goodsList';
    var params = {
      sort: sort,
      sort_asc: sort_asc,
      page: offset
    };
    if (goodstype == 'search') {
      url = '/index.php/api/Goods/search';
      params.q = searchval;
    }
    else {
      params.id = goodstype;
    }
    if (offset > 0) {
      this.setData({
        isLoading: true
      });
    }
    else {
      this.setData({
        isLoading: false
      });
    }
    app.request(url,params).then(this.callBackFuc.bind(this));
  },
  callBackFuc: function (result) {
    app.hideLoading();
    this.setData({
      isLoading: false
    });
    var dataArr = result.result;
    if (dataArr.length <= 0) {
      var blankshow = false;
      if (listOffset == 0) {
        blankshow = true;
      }
      this.setData({
        hasMore: false,
        blankshow: blankshow
      });
      return;
    }
    var orginal_arr = [];
    if (listOffset > 0) {
      orginal_arr = this.data["goodslist"];
    }
    for (var i = 0; i < dataArr.length; i++) {
      var goodsinfo = {};
      goodsinfo.goodsID = dataArr[i].goods_id;
      goodsinfo.sales = dataArr[i].sales_sum;
      if (parseInt(goodsinfo.sales) < 0) {
        goodsinfo.sales = Math.abs(parseInt(goodsinfo.sales));
      }
      goodsinfo.oprice = dataArr[i].market_price;
      goodsinfo.dprice = dataArr[i].shop_price;
      goodsinfo.pic = app.globalData.webUrl + dataArr[i].original_img;

      goodsinfo.name = dataArr[i].goods_name;
      var namestr = goodsinfo.name;
      var long_name = namestr, short_name = namestr;
      if (namestr.length > 15) {
        short_name = namestr.substr(0, 15) + '...';
      }
      if (namestr.length > 30) {
        long_name = namestr.substr(0, 30) + '...';
      }
      goodsinfo.shortname = short_name;
      goodsinfo.longname = long_name;
      orginal_arr.push(goodsinfo);
    }
    this.setData({
      goodslist: orginal_arr
    });
  }
})